<template>
  <div>
    <el-dialog
      :title="title"
      :form="form"
      :visible="state"
      :before-close="quxiao"
    >
      <el-form :model="form">
        <el-form-item label="保险公司名称" :label-width="formLabelWidth">
          <el-input
            v-model="form.insuranceComName"
            autocomplete="off"
            @blur="selectbaoXianGongsiName"
            class="addinput"
          ></el-input>
          <span class="red">{{ cunzai }}</span>
          <span class="green">{{ bucunzai }}</span>
        </el-form-item>
        <el-form-item label="保险公司简称" :label-width="formLabelWidth">
          <el-input
            v-model="form.insuranceComShortName"
            autocomplete="off"
            class="addinput"
          ></el-input>
        </el-form-item>
        <el-form-item label="保险公司报警电话" :label-width="formLabelWidth">
          <el-input
            v-model="form.insuranceComReportPhone"
            autocomplete="off"
            class="addinput"
            type="number"
          ></el-input>
        </el-form-item>
        <el-form-item label="保险公司地址" :label-width="formLabelWidth">
          <el-input
            v-model="form.insuranceComAddress"
            autocomplete="off"
            class="addinput"
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="info" @click="quxiao">取 消</el-button>
        <el-button type="primary" @click="queDing">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import * as api from "../../utils/api";
export default {
  name: "dialogbaoxian",
  props: ["title", "form", "state"],
  data() {
    return {
      formLabelWidth: "150px",
      chaxunName: "",
      chaxunstate: false,
      cunzaiState: 0,
      insurance_com_statusName: "",
      cunzai: "",
      bucunzai: "",
    };
  },
  methods: {
    //关闭弹框
    quxiao() {
      this.$emit("closeDialog");
      this.cunzai = "";
      this.bucunzai = "";
    },
    //点击确定按钮时
    queDing() {
      if (this.title == "修改保险公司信息") {
        //这里发起请求,上传this.upform
        this.dialogFormVisible = false;
        this.axios({
          url: api.POST_UPDATE_BAOXIANGONGSI,
          method: "post",
          data: this.form,
        }).then((data) => {
          // console.log(data);
          if (data.data.code == 200) {
            this.$emit("closeDialog");
            this.$emit("selectAllbaoxian");
            this.$message.success(data.data.msg);
          } else {
            this.$message.error(data.data.msg);
          }
        });
      } else {
        if (
          this.form.insuranceComAddress == "" ||
          this.form.insuranceComName == "" ||
          this.form.insuranceComShortName == "" ||
          this.form.insuranceComReportPhone == ""
        ) {
          this.$message.error("内容没有填完整");
        } else if (this.cunzaiState == 1) {
          this.axios({
            url: api.POST_ADD_BAOXIANGONGSI,
            method: "post",
            data: this.form,
          }).then((data) => {
            if (data.data.code == 200) {
              this.$emit("selectAllbaoxian");
              this.$message.success(data.data.msg);
              this.cunzai = "";
              this.bucunzai = "";
              this.$emit("closeDialog");
            }
          });
          this.dialogFormVisible1 = false;
        } else {
          this.$message.error("公司名重复,不可添加");
        }
      }
    },
    //添加时检测名字
    selectbaoXianGongsiName() {
      if (this.title === "添加保险公司信息") {
        this.cunzaiState = 0;
        this.cunzai = "";
        this.bucunzai = "";
        if (this.form.insuranceComName == "") {
          this.bucunzai = "未输入";
        } else {
          this.axios({
            url: api.GET_SELECT_BAOXIANGONGSI_NAME,
            method: "get",
            params: { insurance_com_name: this.form.insuranceComName },
          }).then((data) => {
            // console.log(data);
            // console.log(this.form.insuranceComName);
            if (data.data.code == 200) {
              this.cunzai = data.data.msg;
            } else {
              this.cunzaiState = 1;
              this.bucunzai = data.data.msg;
            }
          });
        }
      }
    },
  },
};
</script>

<style lang = 'less' scope>
.red {
  color: red;
}
.green {
  color: green;
}
.addinput {
  width: 36rem;
}
</style>